<import resource="classpath:alfresco/webscripts/org/alfresco/confluence/util/utils.js">

// Connect to remote Alfresco document service

// Prepare a remote connector to Alfreso service
var alfDocServiceUrl = "/api";

if ( args.id != null ) {

	alfDocServiceUrl += "/node/"+args.id.replace(":/", "");

}

if ( args.path != null ) {

	alfDocServiceUrl += "/path/workspace/SpacesStore"+args.path;

}

var alfEndpoint = CMISUtils.endpoint.getEndpoint();

var connector = remote.connect(alfEndpoint);

//var ticket = CMISUtils.ticket.getTicket(connector);

//var result = connector.call(alfDocServiceUrl);

var s = new Object();

try {

	// retrieve doc
	var queryResult = connector.call(stringUtils.urlEncodeComponent(alfDocServiceUrl));

	if (queryResult.status == 200) {

	   s.node = atom.toEntry(queryResult.response);
	   
	} else {

	   s.error = queryResult.response;

	}


} catch (e) {
 
 	s.error = "ERROR: " + e + "<br/><br/>";

    if (e.rhinoException != null) {
      
        var re = e.rhinoException;
           //model.iftlresult += "Stack Trace: <br/><br/>";

         var stackTrace = re.getStackTrace() ;
         var out2 = " ";
         for each (n in stackTrace) {         
           out2 += n + "<br>&nbsp;&nbsp;&nbsp;";
        }
           s.error += out2 + "<br/>";
    }

}

model.s = s;
model.endpoint = alfEndpoint;
model.endpointurl = connector.getDescriptor().getEndpointUrl();
 
/*

var alfDoc = CMISUtils.xml.getXml(result);

var docProps = {};

var cmisNS = CMISUtils.ns.cmisNS;
var atomNS = CMISUtils.ns.atomNS;
var alfNS  = CMISUtils.ns.alfNS;

var docName = alfDoc..atomNS::title;
var docDesc = alfDoc..atomNS::summary;
var docLink = alfDoc..atomNS::link.(@rel == "stream").@href;
var docAuthor = alfDoc..atomNS::author.atomNS::name;
var docIcon = alfDoc..alfNS::icon;
var docUpdated = alfDoc..atomNS::updated;

docProps.name    = docName.toString();
docProps.desc    = docDesc.toString();
docProps.link    = docLink.toString()+"?alf_ticket="+ticket;
docProps.author  = docAuthor.toString();
docProps.icon    = docIcon.toString();
docProps.updated = docUpdated.toString();

model.result = docProps;

model.url = alfDocServiceUrl;

*/